package com.lionli.netty.demo.rpc.netty;



import com.lionli.netty.demo.rpc.customer.ClientBootstrap;
import com.lionli.netty.demo.rpc.provider.HelloServiceImpl;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * 服务端业务处理
 *
 * @author LionLi
 */
public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        System.out.println("msg => " + msg);
        // 定义一个协议,比如: "HelloService#hello#你好"
        if(msg.toString().startsWith(ClientBootstrap.providerName)) {
            // 截取协议后的内容
            String result = new HelloServiceImpl().hello(msg.toString().substring(msg.toString().lastIndexOf("#") + 1));
            ctx.writeAndFlush(result);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        ctx.close();
    }
}
